From: David Härdeman Date: Sat, 8 Nov 2025 13:40:39 +0000 (+0100) Subject: statefiles: move dhcpv6_ia_enum_addrs() to odhcpd.c X-Git-Url: http://git.openwrt.org/%22https:/collectd.org//%22http:/www.crowdsec.net/%22/%22https:/collectd.org/%22http:/www.crowdsec.net/%22?a=commitdiff_plain;h=bb39f088f3ef57473df1fe787761bf6654ff19d7;p=project%2Fodhcpd.git statefiles: move dhcpv6_ia_enum_addrs() to odhcpd.c dhcpv6_ia_enum_addrs() is used in several different places (ubus, statesfile, dhcpv6-ia), so move it to a more central location. At the same time, rename it to odhcpd_enum_addr6(). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/302 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index 1729a05..f5a22d2 100644 --- a/src/dhcpv6-ia.c +++ b/src/dhcpv6-ia.c @@ -884,7 +884,7 @@ static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now, .buf_len = sizeof(leasebuf), .buf_idx = 0 }; - dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_log_ia_addr, &ctxt); + odhcpd_enum_addr6(iface, a, now, dhcpv6_log_ia_addr, &ctxt); } info("DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA", diff --git a/src/odhcpd.c b/src/odhcpd.c index df57a49..49912ed 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -42,7 +42,9 @@ #include #include + #include "odhcpd.h" +#include "dhcpv6-ia.h" static int ioctl_sock = -1; @@ -663,6 +665,63 @@ void odhcpd_bmemcpy(void *av, const void *bv, size_t bits) } } +void odhcpd_enum_addr6(struct interface *iface, struct dhcpv6_lease *lease, + time_t now, odhcpd_enum_addr6_cb_t func, void *arg) +{ + struct odhcpd_ipaddr *addrs = iface->addr6; + size_t m = get_preferred_addr(addrs, iface->addr6_len); + + for (size_t i = 0; i < iface->addr6_len; ++i) { + struct in6_addr addr; + uint32_t preferred_lt, valid_lt; + int prefix = lease->length; + + if (!valid_addr(&addrs[i], now)) + continue; + + /* Filter Out Prefixes */ + if (ADDR_MATCH_PIO_FILTER(&addrs[i], iface)) { + char addrbuf[INET6_ADDRSTRLEN]; + info("Address %s filtered out on %s", + inet_ntop(AF_INET6, &addrs[i].addr.in6, addrbuf, sizeof(addrbuf)), + iface->name); + continue; + } + + if (lease->flags & OAF_DHCPV6_NA) { + if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs)) + continue; + + addr = in6_from_prefix_and_iid(&addrs[i], lease->assigned_host_id); + } else { + if (!valid_prefix_length(lease, addrs[i].prefix)) + continue; + + addr = addrs[i].addr.in6; + addr.s6_addr32[1] |= htonl(lease->assigned_subnet_id); + addr.s6_addr32[2] = addr.s6_addr32[3] = 0; + } + + preferred_lt = addrs[i].preferred_lt; + if (preferred_lt > (uint32_t)lease->preferred_until) + preferred_lt = lease->preferred_until; + + if (preferred_lt > (uint32_t)lease->valid_until) + preferred_lt = lease->valid_until; + + if (preferred_lt != UINT32_MAX) + preferred_lt -= now; + + valid_lt = addrs[i].valid_lt; + if (valid_lt > (uint32_t)lease->valid_until) + valid_lt = lease->valid_until; + + if (valid_lt != UINT32_MAX) + valid_lt -= now; + + func(lease, &addr, prefix, preferred_lt, valid_lt, arg); + } +} int odhcpd_parse_addr6_prefix(const char *str, struct in6_addr *addr, uint8_t *prefix) { diff --git a/src/odhcpd.h b/src/odhcpd.h index 613f8f5..5eaf4ce 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -534,11 +534,12 @@ const char *odhcpd_print_mac(const uint8_t *mac, const size_t len); int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits); void odhcpd_bmemcpy(void *av, const void *bv, size_t bits); -typedef void (*dhcpv6_binding_cb_handler_t)(struct dhcpv6_lease *lease, - struct in6_addr *addr, int prefix, - uint32_t pref, uint32_t valid, - void *arg); - +typedef void (*odhcpd_enum_addr6_cb_t)(struct dhcpv6_lease *lease, + struct in6_addr *addr, int prefix, + uint32_t pref, uint32_t valid, + void *arg); +void odhcpd_enum_addr6(struct interface *iface, struct dhcpv6_lease *lease, + time_t now, odhcpd_enum_addr6_cb_t func, void *arg); int odhcpd_parse_addr6_prefix(const char *str, struct in6_addr *addr, uint8_t *prefix); int odhcpd_netmask2bitlen(bool v6, void *mask); bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask); diff --git a/src/statefiles.c b/src/statefiles.c index d0adb09..c3476e4 100644 --- a/src/statefiles.c +++ b/src/statefiles.c @@ -30,64 +30,6 @@ static uint8_t statemd5[16]; -void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcpv6_lease *lease, - time_t now, dhcpv6_binding_cb_handler_t func, void *arg) -{ - struct odhcpd_ipaddr *addrs = iface->addr6; - size_t m = get_preferred_addr(addrs, iface->addr6_len); - - for (size_t i = 0; i < iface->addr6_len; ++i) { - struct in6_addr addr; - uint32_t preferred_lt, valid_lt; - int prefix = lease->length; - - if (!valid_addr(&addrs[i], now)) - continue; - - /* Filter Out Prefixes */ - if (ADDR_MATCH_PIO_FILTER(&addrs[i], iface)) { - char addrbuf[INET6_ADDRSTRLEN]; - info("Address %s filtered out on %s", - inet_ntop(AF_INET6, &addrs[i].addr.in6, addrbuf, sizeof(addrbuf)), - iface->name); - continue; - } - - if (lease->flags & OAF_DHCPV6_NA) { - if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs)) - continue; - - addr = in6_from_prefix_and_iid(&addrs[i], lease->assigned_host_id); - } else { - if (!valid_prefix_length(lease, addrs[i].prefix)) - continue; - - addr = addrs[i].addr.in6; - addr.s6_addr32[1] |= htonl(lease->assigned_subnet_id); - addr.s6_addr32[2] = addr.s6_addr32[3] = 0; - } - - preferred_lt = addrs[i].preferred_lt; - if (preferred_lt > (uint32_t)lease->preferred_until) - preferred_lt = lease->preferred_until; - - if (preferred_lt > (uint32_t)lease->valid_until) - preferred_lt = lease->valid_until; - - if (preferred_lt != UINT32_MAX) - preferred_lt -= now; - - valid_lt = addrs[i].valid_lt; - if (valid_lt > (uint32_t)lease->valid_until) - valid_lt = lease->valid_until; - - if (valid_lt != UINT32_MAX) - valid_lt -= now; - - func(lease, &addr, prefix, preferred_lt, valid_lt, arg); - } -} - struct write_ctxt { FILE *fp; md5_ctx_t md5; @@ -182,8 +124,8 @@ static void statefiles_write_hosts(time_t now) continue; if (INFINITE_VALID(lease->valid_until) || lease->valid_until > now) - dhcpv6_ia_enum_addrs(ctxt.iface, lease, now, - dhcpv6_write_ia_addrhosts, &ctxt); + odhcpd_enum_addr6(ctxt.iface, lease, now, + dhcpv6_write_ia_addrhosts, &ctxt); } } @@ -251,7 +193,7 @@ static void statefiles_write_dhcpv6_lease(struct write_ctxt *ctxt, struct dhcpv6 lease->assigned_subnet_id, lease->length); if (INFINITE_VALID(lease->valid_until) || lease->valid_until > ctxt->now) - dhcpv6_ia_enum_addrs(ctxt->iface, lease, ctxt->now, dhcpv6_write_ia_addr, ctxt); + odhcpd_enum_addr6(ctxt->iface, lease, ctxt->now, dhcpv6_write_ia_addr, ctxt); ctxt->buf[ctxt->buf_idx - 1] = '\n'; fwrite(ctxt->buf, 1, ctxt->buf_idx, ctxt->fp); diff --git a/src/statefiles.h b/src/statefiles.h index f6fc933..9acd9ab 100644 --- a/src/statefiles.h +++ b/src/statefiles.h @@ -7,9 +7,6 @@ #ifndef _STATEFILES_H_ #define _STATEFILES_H_ -void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcpv6_lease *lease, - time_t now, dhcpv6_binding_cb_handler_t func, void *arg); - bool statefiles_write(void); #endif /* _STATEFILES_H_ */ diff --git a/src/ubus.c b/src/ubus.c index 137605e..e589d4e 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -153,7 +153,7 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct blobmsg_close_array(&b, m); m = blobmsg_open_array(&b, a->flags & OAF_DHCPV6_NA ? "ipv6-addr": "ipv6-prefix"); - dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL); + odhcpd_enum_addr6(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL); blobmsg_close_array(&b, m); blobmsg_add_u32(&b, "valid", INFINITE_VALID(a->valid_until) ?